package com.netease.server.example.web.controller;

import org.apache.log4j.Logger;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by zhanpeng on 16-9-7.
 */
public class LoginServlet extends HttpServlet {

    private static Logger logger = Logger.getLogger(LoginServlet.class);

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        logger.info("Class LoginServlet Method doGet");
        login(req, resp);
//        super.doGet(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        logger.info("Class LoginServlet Method doPost");
        login(req, resp);
//        super.doPost(req, resp);
    }

    @Override
    public void destroy() {
        logger.info("Class LoginServlet Method destroy");
        super.destroy();
    }

    @Override
    public void init() throws ServletException {
        logger.info("Class LoginServlet Method init");
        super.init();
    }

    // lession homework for the title 3
    public void login(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        logger.info("Class LoginServlet Method login");

        //get user & password
        String username = request.getParameter("user");
        String password = request.getParameter("password");

        logger.info("Class LoginServlet Method login [user]:" + username);
        logger.info("Class LoginServlet Method login [password]:" + password);

        // check first login
        Cookie[] cookies = request.getCookies();
        HttpSession password_session = request.getSession();

        Boolean first_login = true;
        if(cookies != null)
            for(Cookie cookie : cookies)
                if(cookie.getName().equals("user"))
                    first_login = false;

        logger.info("Class LoginServlet Method login [first_login]:" + first_login);

        if(first_login == true)
        {
            Cookie username_cookie = new Cookie("user",username);
            password_session.setAttribute("password",password);
            // set cookie max time 30min
            username_cookie.setMaxAge(30 * 60);
            // response cooke
            response.addCookie(username_cookie);
        }
        // if not first login ;set session destory
        else
        {
            password_session.invalidate();
            for(Cookie cookie : cookies)
            {
                if(cookie.getName().equals("user"))
                {
                    if(cookie.getValue().equals(username))
                    {
                        //nothing
                    }
                    else
                    {
                        System.out.println(username);
                    }
                }
            }
        }



        try {
                PrintWriter writer = response.getWriter();
                writer.println("<html>");
                writer.println("<head><title>Login</title></head>");
                writer.println("<body>");
                writer.println("<p>[username]:" + username + "</p>");
                writer.println("<p>[password]:" + password + "</p>");
                writer.println("</body>");
                writer.println("</html>");
                writer.close();
        } catch (Exception e) {
            e.printStackTrace();
            RequestDispatcher dispatcher = null;
            dispatcher = request.getRequestDispatcher("/error.html");
            dispatcher.forward(request, response);
        }

    }
}
